
import { app } from "../../base";
import { MeVo } from "./MeVo";

export class Role {
    private _me: MeVo;

    public async login(): Promise<boolean> {
        return new Promise((resolve: (value: boolean) => void, reject: (reason?: any) => void) => {
            if (app.publisher.login.isSupport()) {
                app.publisher.login.login(this, () => {
                    console.log("Login Success", app.publisher.login.openID);
                    //必须！！开启本地缓存中的角色数据
                    app.storage.userId = <string>app.publisher.login.openID;
                    this.me.init(<string>app.publisher.login.openID);
                    resolve(true);
                }, () => {
                    reject("登录失败");
                })
            } else {
                reject("当前平台不支持账号登录");
            }
        });
    }

    public async logout(): Promise<boolean> {
        return true;
    }

    public get me(): MeVo {
        if (!this._me) {
            this._me = new MeVo();
        }
        return this._me;
    }
}